365B - The Fibonacci Segment - CodeForces Solution


implementation *1100

Please click on ads to support us..

Python Code:

n = int(input())
a = list(map(int, input().split()))

best = 2 if n > 1 else 1

current = 2

if n>2:
    for i in range(2,n):
        if a[i]==a[i-1]+a[i-2]:
            current += 1
        else:
            best = max(best,current)
            current = 2
    best = max(best,current)
print(best)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int  n;
    cin >> n;
    vector<int> nums;
    for(int i = 0;i<n;i++){
        int x;
        cin >> x;
        nums.push_back(x);
    }
    int count = 2;
    int maxn = 0;
    for(int i = 2;i<n;i++){
        if(nums[i]==nums[i-1]+nums[i-2]){
            count+=1;
        }
        else{
            maxn = max(count,maxn);
            count = 2;
        }
    }
    maxn = max(count,maxn);
    if(n==1){
        maxn = 1;
    }
    cout << maxn << endl;
}


Comments

Submit
0 Comments
More Questions

1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection